#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Predesys Server - User Interface - System
@version: 0.2.5
@license: GPL 3.0 (see "license" file)
@author: Jose A. Jimenez
@contact: jajimc@gmail.com"""
   
import sys
sys.dont_write_bytecode = True
import os
import os.path
import getpass
DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(os.path.join(DIR, "../core"))
import security
import usage
import tools
import system
import configuration

def manageArgs(argc, argv):
    """Manages command line input.
    @param argc: Command line argument count
    @type argc: C{int}
    @param argv: Command line input data
    @type argv: C{list}"""
    if argc == 3 and sys.argv[2] == "corepath":
        print_core_path()
    elif argc == 3 and sys.argv[2] == "configure":
        configure_system_interactively()
    elif (argc == 17 or argc == 19) and sys.argv[2] == "configure":
        configure_system(sys.argv)
    elif argc == 3 and sys.argv[2] == "installdb":
        install_db()
    else:
        print usage.get_usage()
        
def print_core_path():
    """Prints Predesys Server Core directory path."""
    print system.get_core_path()
    
def configure_system_interactively():
    """Sets Predesys Server configuration prompting for every field."""
    security.check_root()
        
    ok = False
    while (not ok):
        dbType = raw_input("DB server type (available types: mysql): ")
        ok = (dbType != None and dbType != "")
        if not ok: print "Data not valid, try again"

    dbHost = raw_input("DB server host: ")
    if dbHost == None: dbHost = ""
    
    ok = False
    while (not ok):
        dbPort = raw_input("DB server port: ")
        ok = (dbPort != None and (dbPort == "" or dbPort.isdigit()))
        if not ok: print "Data not valid, try again"
    if dbPort.isdigit(): dbPort = int(dbPort)
    else: dbPort = None
        
    dbName = raw_input("DB name: ")
    if dbName == None: dbName = ""
    
    dbUserName = raw_input("DB user name: ")
    if dbUserName == None: dbUserName = ""
        
    dbUserPassword = getpass.getpass("DB user password: ")
    if dbUserPassword == None: dbUserPassword == ""
        
    webServiceHost = raw_input("Web service host: ")
    if webServiceHost == None: webServiceHost == ""
        
    ok = False
    while (not ok):
        webServicePort = raw_input("Web service port: ")
        ok = (webServicePort != None and webServicePort != "" and webServicePort.isdigit())
        if not ok: print "Data not valid, try again"
    webServicePort = int(webServicePort)
        
    config = configuration.CoreConfiguration()
    config.set_db_type(dbType)
    config.set_db_host(dbHost)
    config.set_db_port(dbPort)
    config.set_db_name(dbName)
    config.set_db_user_name(dbUserName)
    config.set_db_user_password(dbUserPassword)
    config.set_webservice_host(webServiceHost)
    config.set_webservice_port(webServicePort)

    configuration.save_configuration(config)
    
def configure_system(argv):
    """Sets Predesys Server configuration.
    @param argv: Command line input data
    @type argv: C{list}"""
    security.check_root()
    
    dbType = tools.get_parameter_value(argv, "dbt")
    if dbType == None: raise exceptions.Exception("DB server type not valid or not specified")
    
    dbHost = tools.get_parameter_value(argv, "dbh")
    if dbHost == None: dbHost = ""
    
    dbPort = tools.get_parameter_value(argv, "dbp")
    if dbPort != None and not dbPort.isdigit(): raise exceptions.Exception("DB server port not valid")
    if dbPort != None: dbPort = int(dbPort)
    
    dbName = tools.get_parameter_value(argv, "dbn")
    if dbName == None: dbName = ""
    
    dbUserName = tools.get_parameter_value(argv, "dbun")
    if dbUserName == None: dbUserName = ""
    
    dbUserPassword = tools.get_parameter_value(argv, "dbup")
    if dbUserPassword == None: dbUserPassword = ""
    
    webServiceHost = tools.get_parameter_value(argv, "wsh")
    if webServiceHost == None: webServiceHost = ""
    
    webServicePort = tools.get_parameter_value(argv, "wsp")
    if webServicePort == None: raise exceptions.Exception("Web service port not valid or not specified")
    webServicePort = int(webServicePort)
    
    config = configuration.CoreConfiguration()
    config.set_db_type(dbType)
    config.set_db_host(dbHost)
    config.set_db_port(dbPort)
    config.set_db_name(dbName)
    config.set_db_user_name(dbUserName)
    config.set_db_user_password(dbUserPassword)
    config.set_webservice_host(webServiceHost)
    config.set_webservice_port(webServicePort)

    configuration.save_configuration(config)
    
def install_db():
    """Installs Predesys Server data base. Predesys Server must be previously configured."""
    security.check_root()
    system.install_db()
